<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Newsletter extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->tableName = "newsletter";
		$this->prefix = "nlt";
	}

	function getNewsletter($where = NULL, $start = NULL, $limit = NULL, $fields = NULL, $query = NULL, $order = "nltSubject", $dir = "ASC") {

		$fieldsQuery = Common::fieldsquery($fields, $query);

		$qs =	"
					select
						nlt_id, nlt_subject, nlt_text, nlt_sent
					from
						".$this->db->dbprefix.$this->tableName."
					where
						nlt_del = 0 ".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					order by
						".Common::underscore($order)." ".$dir."
					".(($start)?(" limit ".$start.",".$limit):(""))."
					";
		$result = $this->db->query($qs);

		$getNewsletter = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {

				$getNewsletter[] = array(
					"nltId" => $row->nlt_id,
					"nltSubject" => $row->nlt_subject,
					"nltText" => $row->nlt_text,
					"nltSent" => $row->nlt_sent,
				);

			}
		}

		return $getNewsletter;
	}

	function getNewsletterCount($where = NULL, $fields, $query) {

		$fieldsQuery = Common::fieldsquery($fields, $query);

		$qs =	"
					select
						count(*) as cnt
					from
						".$this->db->dbprefix.$this->tableName."
					where
						nlt_del = 0 ".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					";
		$result = $this->db->query($qs);

		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				return $row->cnt;
			}
		}
	}

}